package com.ggs.service.impl;

import com.ggs.mapper.ArticleMapper;
import com.ggs.pojo.Article;
import com.ggs.service.ArticleService;
import com.ggs.utils.ThreadLocalUtils;
import com.ggs.vo.PageVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void add(Article article) {

        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        Map claims = ThreadLocalUtils.get();
        Integer userid = (Integer)claims.get("id");
        article.setCreateUser(userid);
        articleMapper.add(article);
    }

    @Override
    public PageVO list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        // 创建pageVo对象
        PageVO<Article> articlePageVO = new PageVO<>();
        // 使用pageHelper
        PageHelper.startPage(pageNum, pageSize);
        // 赋值给pageVO
        Map claims = ThreadLocalUtils.get();
        Integer createBy = (Integer)claims.get("id");
        List<Article> as = articleMapper.list(categoryId, state, createBy);
        Page<Article> articlePage =(Page <Article>) as;
        long total = articlePage.getTotal();
        List<Article> result = articlePage.getResult();
        articlePageVO.setTotal(total);
        articlePageVO.setItems(result);
        return articlePageVO;
    }

    @Override
    public Article detail(Integer id) {
        return articleMapper.detail(id);
    }

    @Override
    public void update(Article article) {
        articleMapper.update(article);
    }

    @Override
    public void delete(Integer id) {
        articleMapper.delete(id);
    }
}
